<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>gozerlib.persist.persist &mdash; GOZERWAVE v0.1 BETA documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1 BETA',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="GOZERWAVE v0.1 BETA documentation" href="../../index.html" />
    <link rel="next" title="gozerlib.persist.persiststate" href="persiststate.html" />
    <link rel="prev" title="gozerlib.persist.pdol" href="pdol.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="persiststate.html" title="gozerlib.persist.persiststate"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pdol.html" title="gozerlib.persist.pdol"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="gozerlib-persist-persist">
<h1>gozerlib.persist.persist<a class="headerlink" href="#gozerlib-persist-persist" title="Permalink to this headline">¶</a></h1>
<p>object restores data.</p>
<dl class="class">
<dt id="gozerlib.persist.persist.JSONindb">
<em class="property">class </em><tt class="descclassname">gozerlib.persist.persist.</tt><tt class="descname">JSONindb</tt><big>(</big><em>parent=None</em>, <em>key_name=None</em>, <em>_app=None</em>, <em>_from_entity=False</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#gozerlib.persist.persist.JSONindb" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref docutils literal"><span class="pre">google.appengine.ext.db.Model</span></tt></p>
<p>model to store json files in.</p>
<dl class="attribute">
<dt id="gozerlib.persist.persist.JSONindb.content">
<tt class="descname">content</tt><a class="headerlink" href="#gozerlib.persist.persist.JSONindb.content" title="Permalink to this definition">¶</a></dt>
<dd>A string that can be longer than 500 bytes.</dd></dl>

<dl class="attribute">
<dt id="gozerlib.persist.persist.JSONindb.filename">
<tt class="descname">filename</tt><a class="headerlink" href="#gozerlib.persist.persist.JSONindb.filename" title="Permalink to this definition">¶</a></dt>
<dd>A textual property, which can be multi- or single-line.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="gozerlib.persist.persist.Persist">
<em class="property">class </em><tt class="descclassname">gozerlib.persist.persist.</tt><tt class="descname">Persist</tt><big>(</big><em>filename</em>, <em>default={}</em><big>)</big><a class="headerlink" href="#gozerlib.persist.persist.Persist" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/functions.html#object"><tt class="xref docutils literal"><span class="pre">object</span></tt></a></p>
<p>persist data attribute to database backed JSON file.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><em>filename</em> (string) &#8211; filename to store data in</li>
<li><em>default</em> (dict, list, string, integer or None) &#8211; default data incase Persist object is not yet initialised</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">None</p>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="gozerlib.persist.persist.Persist.listall">
<tt class="descname">listall</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.persist.persist.Persist.listall" title="Permalink to this definition">¶</a></dt>
<dd>list all available json files in database.</dd></dl>

<dl class="method">
<dt id="gozerlib.persist.persist.Persist.save">
<tt class="descname">save</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.persist.persist.Persist.save" title="Permalink to this definition">¶</a></dt>
<dd>save json data to database.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="gozerlib.persist.persist.PlugPersist">
<em class="property">class </em><tt class="descclassname">gozerlib.persist.persist.</tt><tt class="descname">PlugPersist</tt><big>(</big><em>filename</em>, <em>default=None</em><big>)</big><a class="headerlink" href="#gozerlib.persist.persist.PlugPersist" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a title="gozerlib.persist.persist.Persist" class="reference internal" href="#gozerlib.persist.persist.Persist"><tt class="xref docutils literal"><span class="pre">gozerlib.persist.persist.Persist</span></tt></a></p>
<p>persist plug related data. data is stored in gozerstore/plugs/{plugname}/{filename}</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><em>filename</em> (string) &#8211; filename to store data in</li>
<li><em>default</em> (dict, list, string, integer or None) &#8211; default data incase Persist object is not yet initialised</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">None</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<div class="section" id="code">
<h2>code<a class="headerlink" href="#code" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117</pre></td><td class="code"><div class="highlight"><pre><span class="c"># gozerlib/persist/persist.py</span>
<span class="c">#</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">     allow data to be written to disk in JSON format. creating the persisted </span>
<span class="sd">    object restores data. </span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="c">## gozerlib imports</span>

<span class="kn">from</span> <span class="nn">gozerlib.utils.trace</span> <span class="kn">import</span> <span class="n">calledfrom</span>
<span class="kn">from</span> <span class="nn">gozerlib.utils.lazydict</span> <span class="kn">import</span> <span class="n">LazyDict</span>

<span class="c">## simplejson imports</span>

<span class="kn">from</span> <span class="nn">simplejson</span> <span class="kn">import</span> <span class="n">load</span><span class="p">,</span> <span class="n">dump</span><span class="p">,</span> <span class="n">loads</span><span class="p">,</span> <span class="n">dumps</span>

<span class="c">## google imports</span>

<span class="kn">from</span> <span class="nn">google.appengine.ext</span> <span class="kn">import</span> <span class="n">db</span>
<span class="kn">from</span> <span class="nn">google.appengine.api.memcache</span> <span class="kn">import</span> <span class="n">get</span><span class="p">,</span> <span class="n">set</span><span class="p">,</span> <span class="n">replace</span>

<span class="c"># basic imports</span>
<span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">import</span> <span class="nn">thread</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">logging</span>

<span class="k">class</span> <span class="nc">JSONindb</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; model to store json files in. &quot;&quot;&quot;</span>

    <span class="n">filename</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">StringProperty</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">content</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">TextProperty</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Persist</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        persist data attribute to database backed JSON file. </span>

<span class="sd">        :param filename: filename to store data in</span>
<span class="sd">        :type filename: string</span>
<span class="sd">        :param default: default data incase Persist object is not yet initialised</span>
<span class="sd">        :type default: dict, list, string, integer or None</span>
<span class="sd">        :rtype: None</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="p">{}):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fn</span> <span class="o">=</span> <span class="nb">unicode</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="c"># filename to save to</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obj</span> <span class="o">=</span> <span class="n">LazyDict</span><span class="p">()</span>
        <span class="n">jsontxt</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">jsontxt</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">obj</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">GqlQuery</span><span class="p">(</span><span class="s">&quot;SELECT * FROM JSONindb WHERE filename = :1&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">default</span>
                <span class="k">return</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">obj</span> <span class="o">==</span> <span class="bp">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">obj</span> <span class="o">=</span> <span class="n">JSONindb</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">default</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">default</span>
                <span class="k">return</span>
            <span class="n">jsontxt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="o">.</span><span class="n">content</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">loads</span><span class="p">(</span><span class="n">jsontxt</span><span class="p">)</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&#39;loaded </span><span class="si">%s</span><span class="s"> (</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">jsontxt</span><span class="p">)))</span>

    <span class="k">def</span> <span class="nf">listall</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; list all available json files in database. &quot;&quot;&quot;</span>

        <span class="n">json</span> <span class="o">=</span> <span class="n">JSONindb</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="n">j</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="mf">1000</span><span class="p">)</span> 
        <span class="n">names</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">j</span><span class="p">:</span>
            <span class="n">names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">names</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; save json data to database. &quot;&quot;&quot;</span>

        <span class="n">bla</span> <span class="o">=</span> <span class="n">dumps</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
        <span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">,</span> <span class="n">bla</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">obj</span> <span class="o">=</span> <span class="n">JSONindb</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="n">bla</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">bla</span>
        <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="o">.</span><span class="n">put</span><span class="p">()</span>  
        <span class="n">logging</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&#39;saved </span><span class="si">%s</span><span class="s"> (</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fn</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">bla</span><span class="p">)))</span>

<span class="k">class</span> <span class="nc">PlugPersist</span><span class="p">(</span><span class="n">Persist</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        persist plug related data. data is stored in gozerstore/plugs/{plugname}/{filename}</span>

<span class="sd">        :param filename: filename to store data in</span>
<span class="sd">        :type filename: string</span>
<span class="sd">        :param default: default data incase Persist object is not yet initialised</span>
<span class="sd">        :type default: dict, list, string, integer or None</span>
<span class="sd">        :rtype: None</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>

        <span class="c"># retrieve plugname where object is constructed</span>
        <span class="n">plugname</span> <span class="o">=</span> <span class="n">calledfrom</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">_getframe</span><span class="p">())</span>

        <span class="c"># call base constructor with appropiate filename</span>
        <span class="n">Persist</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;gozerstore&#39;</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="s">&#39;plugs&#39;</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="n">plugname</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="n">filename</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">gozerlib.persist.persist</a><ul>
<li><a class="reference external" href="#code">code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="pdol.html"
                                  title="previous chapter">gozerlib.persist.pdol</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="persiststate.html"
                                  title="next chapter">gozerlib.persist.persiststate</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../../_sources/gozerlib/persist/persist.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="persiststate.html" title="gozerlib.persist.persiststate"
             >next</a> |</li>
        <li class="right" >
          <a href="pdol.html" title="gozerlib.persist.pdol"
             >previous</a> |</li>
        <li><a href="../../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Bart Thate.
      Last updated on Nov 14, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>